package iso3.pt.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import iso3.pt.model.Alumno;
import iso3.pt.model.Asignatura;
import iso3.pt.model.Evaluacion;
import iso3.pt.service.PtDaoService;

import com.opensymphony.xwork2.ActionContext;



public class StudentAction extends com.opensymphony.xwork2.ActionSupport implements com.opensymphony.xwork2.Preparable {
	private static final long serialVersionUID = 1L;
	
	private Alumno alumno;
	private Asignatura subject;
	private int subjectID;
	private List<Asignatura> enrolledSubjectList;	
	private List<Asignatura> subjectList;	
	private List<Evaluacion> evaluationList;
	
	public String doListing() throws Exception {
		return "success";
	}
	
	@SuppressWarnings("rawtypes")
	public String logout() {
		Map session = ActionContext.getContext().getSession();
	  
	  	if (session.get("user") != null)
	  		session.remove("user"); 
		
		return "logout";
	}

	@Override
	public void prepare() throws Exception {
		PtDaoService dao = new PtDaoService();
		alumno = (Alumno)ActionContext.getContext().getSession().get("user");
		enrolledSubjectList = new ArrayList<Asignatura>();
		for (Asignatura sub: dao.getAsignaturas(alumno.getDni()))
			enrolledSubjectList.add(sub);
	}
	
	public String enroll() {
		PtDaoService dao = new PtDaoService();
		dao.matricular(alumno.getDni(), subjectID);
		return "enroll";
	}
	
	public String unenroll() {
		PtDaoService dao = new PtDaoService();
		dao.desmatricular(alumno.getDni(), subjectID);
		return "unenroll";
	}
	
	public String enrollForm() {
		PtDaoService dao = new PtDaoService();
		subjectList = new ArrayList<Asignatura>();
		for (Asignatura sub: dao.getAsignaturas()) {
			subjectList.add(sub);
		}
		return "enrollForm";
	}
	
	
	public Alumno getAlumno() {
		return alumno;
	}

	public void setAlumno(Alumno alumno) {
		this.alumno = alumno;
	}

	public Asignatura getSubject() {
		return subject;
	}

	public void setSubject(Asignatura subject) {
		this.subject = subject;
	}

	public int getSubjectID() {
		return subjectID;
	}

	public void setSubjectID(int subjectID) {
		this.subjectID = subjectID;
	}

	public List<Asignatura> getEnrolledSubjectList() {
		return enrolledSubjectList;
	}

	public void setEnrolledSubjectList(List<Asignatura> enrolledSubjectList) {
		this.enrolledSubjectList = enrolledSubjectList;
	}

	public List<Asignatura> getSubjectList() {
		return subjectList;
	}

	public void setSubjectList(List<Asignatura> subjectList) {
		this.subjectList = subjectList;
	}

	public List<Evaluacion> getEvaluationList() {
		return evaluationList;
	}

	public void setEvaluationList(List<Evaluacion> evaluationList) {
		this.evaluationList = evaluationList;
	}
}
